﻿Imports DevExpress.XtraScheduler
Imports SIGI.Common.BE

Public Class UC_Agenda

#Region "Attributes"
    Private _MsgBox As frmMessage = Nothing
    Private _AptList As List(Of AppointmentBE) = Nothing
#End Region

#Region "Events"

    Private Sub UC_Agenda_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim wAptLbl As AppointmentLabel = Nothing

        If Me.DesignMode Then Return

        Try
            _MsgBox = New frmMessage
            SplitContainer1.FixedPanel = FixedPanel.Panel1
            SchedulerControl1.Start = Date.Today
            'NOTA: 'Para Importante usamos la ya existente.
            'Para que el color de fontdo de la cita se muestre según su importancia en la agenda.
            'Normal
            wAptLbl = New AppointmentLabel(Color.LightBlue, "Normal", "Normal")
            SchedulerStorage1.Appointments.Labels.Add(wAptLbl)

            'Baja
            wAptLbl = New AppointmentLabel(Color.Silver, "Low", "Baja")
            SchedulerStorage1.Appointments.Labels.Add(wAptLbl)

            'Cargo las citas
            GetAppointments()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub SchedulerControl1_EditAppointmentFormShowing(sender As System.Object, e As DevExpress.XtraScheduler.AppointmentFormEventArgs) Handles SchedulerControl1.EditAppointmentFormShowing
        Dim wFrmApt As frmAppointment = Nothing

        Try
            wFrmApt = New frmAppointment(SchedulerControl1, e.Appointment)
            wFrmApt.ShowDialog()
            e.Handled = True
            'Refresco la agenda
            GetAppointments()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub SchedulerStorage1_AppointmentDeleting(sender As System.Object, e As DevExpress.XtraScheduler.PersistentObjectCancelEventArgs) Handles SchedulerStorage1.AppointmentDeleting
        Dim wAptController As AppointmentController = Nothing
        Dim wAptId As Integer = 0

        Try
            If _MsgBox.ShowMessage("Eliminar Cita", "¿Está seguro de eliminar la Cita?",
                                   frmMessage.MessageType.Question_,
                                   frmMessage.MessageButtons.OkCancel) = frmMessage.MsgResult.Cancel Then
                Return
            End If
            wAptId = DirectCast(e.Object, DevExpress.XtraScheduler.Appointment).Id
            wAptController = New AppointmentController
            wAptController.Delete(wAptId)
            GetAppointments()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub
#End Region

#Region "Methods"
    'Obtiene el listado de citas en la BD
    Private Sub GetAppointments()
        Dim wAptController As AppointmentController = New AppointmentController
        _AptList = wAptController.Search(GlobalVariables.gLoguedUser.UserName)

        SchedulerStorage1.Appointments.DataSource = _AptList
        With SchedulerStorage1.Appointments.Mappings
            .AppointmentId = "Id"
            .Description = "Description"
            .Subject = "Subject"
            .Start = "DateStart"
            .End = "DateEnd"
            .Label = "Importance"
        End With
    End Sub
#End Region
End Class
